<template>
    <view class="component-vip-unlock">

        <view class="vip-list-options">
            <view class="option-item" :class="[item.is_background_color ? 'active' : '']" v-for="item in rechageList"
                :key="item.id" @click="getVip(item)">
                <view class="item-1 text-ellipsis">{{ item.title }}</view>
                <view class="item-2 text-ellipsis"><text class="item-rmb">￥</text>{{ item.price }}</view>
                <view class="item-3" v-if="Number(item.original_price) > 0">
                    <view class="item-3-price">
                        {{ item.original_price }}<view class="text-line"></view>
                    </view>
                </view>
                <view class="recommend" v-if="item.icon_text">{{ item.icon_text }}</view>
            </view>
        </view>

        <view class="purchase-notes" v-if="showNote">
            <view class="header">温馨提示</view>
            <view class="instructions">
                <view>1. VIP会员属于虚拟商品，一经购买不可退换;</view>
                <view>2. 未满18周岁的用户，须在监护人主导并同意下进行相关付费操作;</view>
                <view>3. 如出现付费相关问题，可点击下方“联系客服”进行反馈咨询;</view>
                <view>4. VIP会员仅在本小程序内使用。</view>
            </view>
            <button class="im-btn" open-type="im" :data-im-id="imCount">
                <image class="icon-img" :src="`${imgBaseUrl}im-icon.webp`" />联系客服
            </button>
        </view>
    </view>
</template>

<script setup lang="ts">
import { getOrder, getRechageList, queryOrder } from '@/api';
import { useAppid, useAppInit, useAuth } from '@/hooks';
import { retry, ttpay } from '@/utils/pay';
import { onMounted, ref, type PropType } from 'vue';

const { login } = useAuth()
const emit = defineEmits(['unLockSuccess'])
const props = defineProps({
    showNote: {
        type: Boolean,
        default: false,
    },
    comicObjec: {
        type: Object as PropType<API.BookReading>,
        default: {}
    },
    bookItem: {
        type: Object as PropType<API.BookDetail>,
        default: {}
    }
});
const { config } = useAppInit()
const imCount = ref(config.value?.app.im_account)
console.log('imCount', imCount.value)

const indexItem = ref<API.PackageItem>()
const rechageList = ref<API.PackageItem[]>([])


const getVip = async (item: API.PackageItem) => {
    indexItem.value = item

    const { appName, platform } = tt.getSystemInfoSync();
    // if (platform === 'ios') return uni.showModal({
    //     title: '提示',
    //     content: 'iOS暂不支持购买',
    //     showCancel: false
    // })
    uni.showLoading({
        title: '调起支付中',
        mask: true
    })
    setTimeout(() => {
        uni.hideLoading()
    }, 1000);
    console.log(111, props)
    const { data } = await getOrder({
        appid: useAppid(),
        package_id: indexItem.value.id,
        anid: props.comicObjec?.anime?.id || props.bookItem.id,
        chaps: props.comicObjec?.data?.chaps || props.bookItem.chaps,
    })
    // return setTimeout(() => {
    //     uni.hideLoading()
    //     emit('unLockSuccess')
    //     login()
    // }, 1000);
    const { status, orderId, isPay } = await ttpay(data)
    if (isPay) {
        await retry(async () => {
            const { data: res } = await queryOrder(data.order.id);
            console.log('订单状态：', res)
            if (res.status === 2) {
                uni.hideLoading()
                emit('unLockSuccess')
                login()
            } else {
                throw new Error('支付未完成')
            }
        }, 10, 1000)
    } else { }
}
const getList = async () => {
    const { data } = await getRechageList()
    rechageList.value = data.recharge_package
    indexItem.value = rechageList.value[0]
}

onMounted(() => {
    getList()
})


</script>



<style lang="less" scoped>
.component-vip-unlock {
    background: #151515;
    padding-top: 26rpx;
    width: 100%;


    .vip-list-options {
        width: 100%;
        min-height: 180rpx;
        display: flex;
        flex-flow: row wrap;
        justify-content: flex-start;
        align-items: flex-start;

        .option-item {
            width: 340rpx;
            height: 180rpx;
            background: #212121;
            border-radius: 20rpx 20rpx 20rpx 20rpx;
            margin-left: 22rpx;
            position: relative;
            overflow: hidden;
            margin-top: 20rpx;

            .item-1 {
                font-size: 28rpx;
                color: #FFFFFF;
                text-align: center;
                margin-top: 36rpx;
            }

            .item-2 {
                font-size: 44rpx;
                color: #F7CC77;
                text-align: center;

                .item-rmb {
                    font-size: 24rpx;
                }
            }

            .item-3 {
                width: 100%;
                display: flex;
                justify-content: center;
                align-items: center;

                .item-3-price {
                    color: #727272;
                    font-size: 24rpx;
                    padding: 0 10rpx;
                    position: relative;

                    .text-line {
                        position: absolute;
                        left: 0;
                        top: 50%;
                        width: 100%;
                        height: 2rpx;
                        background: #999999;
                    }
                }

            }

            .recommend {
                position: absolute;
                left: 0;
                top: 0;
                border-radius: 6rpx 0rpx 20rpx 0rpx;
                background: #F7CD77;
                min-width: 60rpx;
                padding: 0 20rpx;
                height: 36rpx;
                font-size: 24rpx;
                color: #B14800;
                display: flex;
                justify-content: center;
                align-items: center;
            }
        }

        .active {
            background: rgb(255, 218, 138);

            .recommend {
                background: linear-gradient(to right, #433827, #5E4B2E, #423727);
                color: #FFF8CF;
            }

            .item-1,
            .item-2 {
                color: #B14800;
            }

            .item-3 .item-3-price {
                color: rgba(177, 72, 0, 0.65);
            }

            .text-line {
                background: rgba(177, 72, 0, 0.65);
            }
        }
    }

    .purchase-notes {
        margin-top: 128rpx;
        padding: 0 24rpx 40rpx;
        font-size: 24rpx;
        color: rgba(255, 255, 255, 0.65);
        line-height: 2;

        .header {
            font-size: 28rpx;
            color: #FFFFFF;
        }

        .im-btn {
            width: 560rpx;
            height: 76rpx;
            border-radius: 38rpx 38rpx 38rpx 38rpx;
            border: 2rpx solid #FFFFFF;
            font-size: 28rpx;
            color: #FFFFFF;
            padding: 0;
            margin-top: 76rpx;
            background: #151515;
            display: flex;
            justify-content: center;
            align-items: center;

            &::after {
                display: none;
            }

            .icon-img {
                width: 44rpx;
                height: 44rpx;
                margin-right: 16rpx;
            }
        }

    }



}
</style>